<!-- 登录 -->
<template>
  <el-form
    ref="form"
    label-width="70px"
    :inline="true"
    class="login-container"
    :model="form"
    :rules="loginFormRules"
  >
    <h3 class="login_title">系统登录</h3>
    <el-form-item label="工号" prop="username">
      <el-input v-model="form.username" placeholder="请输入工号"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input
        type="password"
        v-model="form.password"
        placeholder="请输入密码"
      ></el-input>
    </el-form-item>
    <el-form-item>
      <el-button
        @click="submit"
        style="margin-left: 105px; margin-top: 10px"
        type="primary"
        >登录</el-button
      >
      <el-button type="info" @click="resetLoginForm">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import Cookie from "js-cookie";
import { Login } from "../../api";
export default {
  data() {
    return {
      form: {
        username: "admin",
        password: "ljy123456",
      },

      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  // methods: {
  //   submit() {
  //     const token = Mock.Random.guid();
  //     Cookie.set("token", token);
  //     this.$router.push("/DataBoard");
  //   },
  // },
  methods: {
    // 登录
    submit() {
      // token信息
      // const token = Mock.Random.guid();

      // 校验通过
      this.$refs.form.validate((valid) => {
        if (valid) {
          Login(this.form).then(({ data }) => {
            console.log(data);
            if (data.success === true) {
              // token信息存入cookie用于不同页面间的通信
              Cookie.set("token", data.data);
              // 0;
              // 跳转到首页
              this.$router.push("/DataBoard");
              console.log(Cookie.get("token"));
            } else {
              this.$message.error(data.message);
            }
          });
        }
      });
    },
    // 重置
    resetLoginForm()  {
        // console.log(this);
        this.$refs.form.resetFields();
    },
  },
};
</script>
<style lang="less" scoped>
.login-container {
  width: 350px;
  border: 1px solid #eaeaea;
  margin: 180px auto;
  padding: 35px 35px 15px 35px;
  background-color: #fff;
  border-radius: 15px;
  box-shadow: 0 0 25px #cac6c6;
  box-sizing: border-box;
}
.login_title {
  text-align: center;
  margin-bottom: 40px;
  color: #505458;
}
.el-input {
  width: 198px;
}
</style>
